package com.ymr.iot.consumer.service;

import com.ymr.iot.consumer.config.IotAccountInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.jms.Message;

@Service
@Component
public class IotMessageService {
    private final static Logger logger = LoggerFactory.getLogger(IotMessageService.class);

    @Autowired
    private IotAccountInfo iotAccountInfo;

    public void init() throws Exception {

    }

    /**
     * 在这里处理您收到消息后的具体业务逻辑。
     */
    @Async
    public void processMessage(Message message) {
        try {
            byte[] body = message.getBody(byte[].class);
            String content = new String(body);
            String topic = message.getStringProperty("topic");
            String messageId = message.getStringProperty("messageId");
            logger.info("receive message"
                    + ", topic = " + topic
                    + ", messageId = " + messageId
                    + ", content = " + content);
        } catch (Exception e) {
            logger.error("处理消息时出错", e);
        }
    }
}
